package com.gy.config;

import com.gy.controller.assistant.MongoChatMemoryStore;
import com.gy.controller.assistant.XiaoNingAgent;
import com.gy.controller.assistant.tools.PaperTool;
import com.gy.controller.assistant.tools.PatentTool;
import com.gy.entity.result.Patents;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.service.AiServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class XiaoningAgentConfig {

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Value("${dashscope.api-key:sk-5f83eb74be6b4fe49c0a62b03294db1e}")
    private String dashScopeApiKey;
    @Value("${dashscope.model-name:qwen-max}")
    private String qwenModelName;

    @Bean
    public QwenStreamingChatModel streamingChatModel() {
        return QwenStreamingChatModel.builder()
                .apiKey(dashScopeApiKey)
                .modelName(qwenModelName)
                .temperature(0.1F)
                .build();
    }

    @Bean
    public ChatMemoryProvider chatMemoryProvider() {
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(mongoChatMemoryStore)
                .build();
    }

    // 新增：非流式模型（用于表单字段提取，确保完整JSON）
    @Bean
    public QwenChatModel nonStreamingChatModel() {
        return QwenChatModel.builder()
                .apiKey(dashScopeApiKey)
                .modelName(qwenModelName) // 用同一模型，非流式版本
                .temperature(0.0F) // 0.0更严谨，避免AI发挥，确保JSON格式正确
                .build();
    }

}
